package com.example.planetexploder.module.firstpage;

import android.content.Context;
import android.util.Log;

import com.example.planetexploder.basepresenter.PBook;
import com.example.planetexploder.basepresenter.PSort;
import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.constants.LogConst;
import com.example.planetexploder.model.dao.Dao;
import com.example.planetexploder.model.folder.Folder;
import com.example.planetexploder.model.preference.SharedPreference;
import com.example.planetexploder.utils.IdCreator;
import com.example.planetexploder.utils.MyRandom;
import com.example.planetexploder.utils.TimeUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 首页Presenter
 * @author 蒋子涵
 * 修改日期：2019.10.22
 */
public class Presenter_FirstPage {

    private static final String TAG = "Presenter_FirstPage";

    private Context context;

    private Dao dao;
    private SharedPreference sp;
    private Folder folder;

    public PBook pBook;
    public PSort pSort;

    public Presenter_FirstPage(Context context) {
        dao = new Dao(context);
        sp = new SharedPreference(context);
        folder = new Folder(context);
        pBook = new PBook(context);
        pSort = new PSort(context);
        this.context = context;
    }

    /**
     * 获取首页摘抄集的信息
     * @return      首页摘抄集的bookInfo
     */
    public BookInfo get_FirstPageBookInfo() throws Exception {
        Log.d(LogConst.PTAG, "get_FirstPageBookInfo()");
        //如果是首次打开应用，则为书架中添加一本样例摘抄集，并将其设置为首页摘抄集
        if (sp.appSettings.isFirstTime()==0) {
            BookInfo bookInfo = new BookInfo();
            bookInfo.dateMod = TimeUtil.getCurrentTime();
            bookInfo.bookName = "样例书本";
            bookInfo.bookId = IdCreator.create_BookId();
            bookInfo.tag1 = BookInfo.TAG_READING;
            bookInfo.tag2 = sp.classifications.get_MaxID();
            bookInfo.tag3 = MyRandom.getColorTag();

            sp.classifications.save_Classification("样例分类");
            sp.appSettings.set_FirstPageBook(bookInfo.bookId);

            dao.bookTable.create_BookTable(bookInfo.bookId);
            dao.shelfTable.add_BookInfo_To_ShelfTable(bookInfo);

            folder.bookFolder.create_BookFolder(bookInfo.bookId);

            ItemInfo itemInfo = new ItemInfo();
            itemInfo.itemName = "这是一条样例摘抄";
            itemInfo.dateMod = TimeUtil.getCurrentTime();
            itemInfo.itemId = IdCreator.create_TextId();

            dao.bookTable.add_ItemInfo_To_BookTable(bookInfo.bookId, itemInfo);

            folder.textFile.save_Text(bookInfo.bookId, itemInfo.itemId, "这是一条样例摘抄，您可以在首页或书本的页面中为一本书添加摘抄");
            sp.appSettings.set_SecondTime();

            //首次打开应用时创建拍照暂存图片
            File dir = context.getFilesDir();
            File imageFile = new File(dir,"camera.jpg");
            if(!imageFile.exists()) {
                try {
                    imageFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return dao.shelfTable.get_BookInfo(sp.appSettings.get_FirstPageBook());
    }

    /**
     * 获取首页摘抄列表
     * @return      首页摘抄集的所有摘抄
     */
    public ArrayList<ItemInfo> get_FirstPageItems() {
        Log.d(LogConst.PTAG, "get_FirstPageItems()");
        return pBook.get_BookTable(sp.appSettings.get_FirstPageBook(),sp.appSettings.get_SortWay());
    }

}
